<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Squid," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;centos 系统自带 squid 包： 1[root@192 ~]# yum install -y squid &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;当然也可以源码包编译安装，Squid官方网站，以下为 squid 编译参数 123456789">
<meta name="keywords" content="Squid">
<meta property="og:type" content="article">
<meta property="og:title" content="squid正向代理">
<meta property="og:url" content="https://hcldirgit.github.io/2017/09/03/Squid/2. squid正向代理/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;centos 系统自带 squid 包： 1[root@192 ~]# yum install -y squid &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;当然也可以源码包编译安装，Squid官方网站，以下为 squid 编译参数 123456789">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/01.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/02.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/03.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/04.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/05.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/06.png?raw=true">
<meta property="og:updated_time" content="2017-09-02T17:35:14.920Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="squid正向代理">
<meta name="twitter:description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;centos 系统自带 squid 包： 1[root@192 ~]# yum install -y squid &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;当然也可以源码包编译安装，Squid官方网站，以下为 squid 编译参数 123456789">
<meta name="twitter:image" content="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/01.png?raw=true">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/09/03/Squid/2. squid正向代理/"/>





  <title>squid正向代理 | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/09/03/Squid/2. squid正向代理/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">squid正向代理</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-03T02:13:44+08:00">
                2017-09-03
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;centos 系统自带 squid 包：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># yum install -y squid</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当然也可以源码包编译安装，<a href="http://www.squid-cache.org/" target="_blank" rel="external">Squid官方网站</a>，以下为 squid 编译参数</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">./configure --prefix=/usr/<span class="built_in">local</span>/squid \</div><div class="line">--<span class="built_in">disable</span>-dependency-tracking \</div><div class="line">--<span class="built_in">enable</span>-dlmalloc \</div><div class="line">--<span class="built_in">enable</span>-gnuregex \</div><div class="line">--<span class="built_in">disable</span>-carp \</div><div class="line">--<span class="built_in">enable</span>-async-io=240 \</div><div class="line">--with-pthreads \</div><div class="line">--<span class="built_in">enable</span>-storeio=ufs,aufs,diskd,null \</div><div class="line">--<span class="built_in">disable</span>-wccp \</div><div class="line">--<span class="built_in">disable</span>-wccpv2 \</div><div class="line">--<span class="built_in">enable</span>-kill-parent-hack \</div><div class="line">--<span class="built_in">enable</span>-cachemgr-hostname=localhost \</div><div class="line">--<span class="built_in">enable</span>-default-err-language=Simplify_Chinese \</div><div class="line">--with-build-environment=POSIX_V6_ILP32_OFFBIG \</div><div class="line">--with-maxfd=65535 \</div><div class="line">--with-aio \</div><div class="line">--<span class="built_in">disable</span>-poll \</div><div class="line">--<span class="built_in">enable</span>-epoll \</div><div class="line">--<span class="built_in">enable</span>-linux-netfilter \</div><div class="line">--<span class="built_in">enable</span>-large-cache-files \</div><div class="line">--<span class="built_in">disable</span>-ident-lookups \</div><div class="line">--<span class="built_in">enable</span>-default-hostsfile=/etc/hosts \</div><div class="line">--with-dl \</div><div class="line">--with-large-files \</div><div class="line">--<span class="built_in">enable</span>-removal-policies=heap,lru \</div><div class="line">--<span class="built_in">enable</span>-delay-pools \</div><div class="line">--<span class="built_in">enable</span>-snmp \</div><div class="line">--<span class="built_in">disable</span>-internal-dns</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用 centos 自带的 squid 已经满足需求，所以没必要编译安装。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装完成后，可以查看 squid 版本：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># squid -v</span></div><div class="line">Squid Cache: Version 3.1.23</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;同时还可以看到 squid 的编译参数。下面配置 squid，来实现正向代理：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># rm -f /etc/squid/squid.conf</span></div><div class="line">[root@192 ~]<span class="comment"># vim /etc/squid/squid.conf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;不实用默认的配置文件，删除以后重新写入以下配置：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">http_port 3128</div><div class="line">acl manager proto cache_object</div><div class="line">acl localhost src 127.0.0.1/32 ::1</div><div class="line">acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1</div><div class="line">acl localnet src 10.0.0.0/8     <span class="comment"># RFC1918 possible internal network</span></div><div class="line">acl localnet src 172.16.0.0/12  <span class="comment"># RFC1918 possible internal network</span></div><div class="line">acl localnet src 192.168.0.0/16 <span class="comment"># RFC1918 possible internal network</span></div><div class="line">acl SSL_ports port 443</div><div class="line">acl Safe_ports port 80 8080 <span class="comment"># http</span></div><div class="line">acl Safe_ports port 21 <span class="comment"># ftp</span></div><div class="line">acl Safe_ports port 443 <span class="comment"># https</span></div><div class="line">acl CONNECT method CONNECT</div><div class="line">http_access allow manager localhost</div><div class="line">http_access deny manager</div><div class="line">http_access deny !Safe_ports</div><div class="line">http_access deny CONNECT !SSL_ports</div><div class="line">http_access allow localnet</div><div class="line">http_access allow localhost</div><div class="line">http_access allow all</div><div class="line">cache_dir aufs /data/cache 1024 16 256</div><div class="line">cache_mem 128 MB</div><div class="line">hierarchy_stoplist cgi-bin ?</div><div class="line">coredump_dir /var/spool/squid</div><div class="line">refresh_pattern ^ftp: 1440 20% 10080</div><div class="line">refresh_pattern ^gopher: 1440 0% 1440</div><div class="line">refresh_pattern -i (/cgi-bin/|\?) 0 0% 0</div><div class="line">refresh_pattern \.(jpg|png|gif|mp3|xml) 1440 50% 2880 ignore-reload</div><div class="line">refresh_pattern . 0 20% 4320</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置文件中，第一行的 “http_port 3128” 这个指的是，squid 服务启动后将要监听的端口，也可以是 80 。“cache_dir” 这个用来指定本地磁盘上的缓存目录，后边的1024为大小，单位是 M 具体根据磁盘大小决定。“cache_mem”它用来规定缓存占用内存的大小，即把缓存的东西存到内存里，具体也需要根据机器的内存定，如果只跑 squid 服务，那么留给系统 512M 内存外，其他可以都分给 squid。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置文件保存后，可以先检测一下是否有语法错误：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># squid -kcheck</span></div><div class="line">squid: ERROR: No running copy</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果提示 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">squid: ERROR: No running copy</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这是 squid 还未启动，不过没有关系，显示成这样说明配置文件没有问题了。在启动前还得初始化缓存目录：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># mkdir /data/cache</span></div><div class="line">[root@192 ~]<span class="comment"># chown -R squid:squid /data/cache</span></div><div class="line">[root@192 ~]<span class="comment"># squid -z</span></div><div class="line">2017/03/30 20:36:42| Creating Swap Directories</div><div class="line">2017/03/30 20:36:42| /data/cache exists</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/00</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/01</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/02</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/03</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/04</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/05</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/06</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/07</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/08</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/09</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/0A</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/0B</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/0C</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/0D</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/0E</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/0F</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;初始化完成后，就可以启动 squid 了：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># /etc/init.d/squid start</span></div><div class="line">正在启动 squid：. [确定]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看 squid 是否启动：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># ps aux | grep squid</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/01.png?raw=true" alt=""></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;现在可以在真机上测测看 squid 的正向代理了，需要设置 IE 选项，或者直接用 curl 命令测试：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># curl -xlocalhost:3128 http://www.baidu.com</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/02.png?raw=true" alt=""></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果看到这样一大串，说明 squid 正向代理设置 OK 了，另外也可以观察 squid 对图片的缓存</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># curl -xlocalhost:3128 http://www.aminglinux.com/bbs/static/image/common/logo.png -I</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/03.png?raw=true" alt=""></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># curl -xlocalhost:3128 http://www.aminglinux.com/bbs/static/image/common/logo.png -I</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/04.png?raw=true" alt=""></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;连续访问两次论坛的 logo 图片，可以发现前后两次的不同，其中“X-Cache-Lookup: HIT from 192.168.0.73:3128”显示，该请求已经 HIT ，它直接从本地的3128端口获取了数据。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有时候还有这样的需求，就是想限制某些域名不能通过代理访问，或者说只想代理某几个域名，在 squid.conf 中找到：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">acl CONNECT method CONNECT</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在其下面添加四行：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">acl http proto HTTP</div><div class="line">acl good_domain dstdomain .apelearn.com .aminglinux.com</div><div class="line">http_access allow http good_domain</div><div class="line">http_access deny http !good_domain</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中白名单域名为“.apelearn.com .aminglinux.com”，这里的 . 表示万能匹配。前面可以是任何字符，只需要填写白名单域名即可。重启 squid 再来测测：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># /etc/init.d/squid restart</span></div><div class="line">[root@192 ~]<span class="comment"># curl -xlocalhost:3128 http://www.baidu.com/ -I</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/05.png?raw=true" alt=""></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;访问百度已经变为403了，如果要设置黑名单，道理是一样的</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">acl http proto HTTP</div><div class="line">acl bad_domain dstdomain .sina.com .souhu.com</div><div class="line">http_access allow http !bad_domain</div><div class="line">http_access deny http bad_domain</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启 squid 后，测试：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># /etc/init.d/squid restart </span></div><div class="line">[root@192 ~]<span class="comment"># curl -xlocalhost:3128 http://www.sina.com/ -I</span></div><div class="line">[root@192 ~]<span class="comment"># curl -xlocalhost:3128 http://www.baidu.com/ -I</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/06.png?raw=true" alt=""></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;baidu.com 可以访问，而 sina.com 不可以访问了</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Squid/" rel="tag"># Squid</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/03/Squid/16. 学习CDN不得不读之-Squid 高级优化指南/" rel="next" title="学习CDN不得不读之-Squid 高级优化指南">
                <i class="fa fa-chevron-left"></i> 学习CDN不得不读之-Squid 高级优化指南
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/03/Squid/3. squid反向代理/" rel="prev" title="squid反向代理">
                squid反向代理 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      

      <section class="site-overview sidebar-panel sidebar-panel-active">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
